﻿using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.TagHelpers.LayUI;
using Microsoft.AspNetCore.ResponseCompression;
using System.Linq;
using ERCS.Model;
using Microsoft.AspNetCore.Mvc.Filters;

namespace ERCS
{
    public class Program
    {

        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateWebHostBuilder(string[] args)
        {

            return
                Host.CreateDefaultBuilder(args)
                 .ConfigureWebHostDefaults(webBuilder =>
                 {
                     webBuilder.ConfigureServices(x =>
                    {


                        var pris = new List<IDataPrivilege>
                        {
                            //new DataPrivilegeInfo<City>("地点", y => y.Name),
                            //new DataPrivilegeInfo<Virus>("病毒", y => y.VirusName),
                        };
                        x.AddFrameworkService(dataPrivilegeSettings: pris);

                        x.AddResponseCompression(opts =>
                        {
                            opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                                new[] { "application/octet-stream" });
                        });

                        x.AddLayui();
                        x.AddSwaggerGen(c =>
                        {
                            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
                            var bearer = new OpenApiSecurityScheme()
                            {
                                Description = "JWT Bearer",
                                Name = "Authorization",
                                In = ParameterLocation.Header,
                                Type = SecuritySchemeType.ApiKey

                            };
                            c.AddSecurityDefinition("Bearer", bearer);
                            var sr = new OpenApiSecurityRequirement();
                            sr.Add(new OpenApiSecurityScheme
                            {
                                Reference = new OpenApiReference
                                {
                                    Type = ReferenceType.SecurityScheme,
                                    Id = "Bearer"
                                }
                            }, new string[] { });
                            c.AddSecurityRequirement(sr);
                        });
                        x.AddRazorPages().AddRazorRuntimeCompilation();
                    });
                     webBuilder.Configure(x =>
                     {
                         var configs = x.ApplicationServices.GetRequiredService<Configs>();
                         if (configs.IsQuickDebug == true)
                         {
                             x.UseSwagger();
                             x.UseSwaggerUI(c =>
                             {
                                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                             });
                         }
                         x.UseFrameworkService();
                         x.UseClientSideBlazorFiles<BlazorApp1.Program>();
                         x.UseEndpoints(endpoints =>
                         {
                             endpoints.MapFallbackToClientSideBlazor<BlazorApp1.Program>("/Client/{**page}", "Index.html");
                         });
                     });
                 }
                 );
        }

        public static string CSSelector(ActionExecutingContext context)
        {
            var userinfo = (context.Controller as IBaseController)?.LoginUserInfo;
            if (userinfo == null)
            {
                return "default";
            }
            else
            {
                Hospital hospital = userinfo.Attributes["Hospital"] as Hospital;
                if (hospital.Location.Name == "abc")
                {
                    return "abc";
                }
                else
                {
                    return "default";
                }
            }
        }

    }
}
